import * as rq from 'request-promise';
import env from './../config/env';
import userInfoModel from './../model/userInfoModel';
import rolesModel from './../model/rolesModel';
import loginUtils from './../core/LoginUtils';
import * as jwt from 'jsonwebtoken';



export class MotorService {

    /**
     * 登录
     * @param res 
     */
    async login(res){

        console.log('resquest:',res);
        
        var username:string = res.username;
        var pwd:string = res.password;

        let json:any = await rq.post(`${env.testUrl}/service/CheckUser`,{form:{code:username, pwd:pwd},json:true}); 

        console.log('登录者的ｊｓｏｎ信息__________________',json);
        
        /**************************用户信息加密**************************/ 
        let users:any = {username:username,pwd:pwd};
        let date = new Date().getTime();
        let token = jwt.sign(users, env.noncestr, { expiresIn: env.expiresIn },date);
        /**************************************************************/ 
        
        if(json.rtcode == '1'){

            await userInfoModel.destroy({where:{loginName:username,pwd:pwd}});
            await rolesModel.destroy({where:{loginName:username,pwd:pwd}});

            let users :any[] = [];

            json.stores.map(item => {
                users.push({
                    userToken : token,
                    loginName : username,
                    pwd:pwd,
                    storName: item.storname,
                    storCode: item.storcode
                })
            })

            let user_result = await userInfoModel.bulkCreate(users);

            let roles :any[] = [];
            json.roles.map((item,i) =>{
                roles[i] = {
                    loginName : username,
                    pwd:pwd,
                    roleName : item.rolename,
                    roleCode : item.rolecode
                }
            });
            
            await rolesModel.bulkCreate(roles);
            return { token : token, rtcode: json.rtcode ,rtmsg : json.rtmsg ,stores : json.stores};
        }


        //失败
        return json;
    }

    /**
     * 仓库管理系统
     * 登录者数据请求
     */
    async show_module(authorization){

        //await loginUtils.checkUser(authorization);
        //console.log('_authorization_________________',authorization);
        
        let user_info = loginUtils.decode(authorization);
        console.log('userINfo__________________',user_info);
        
        let loginName :string = user_info.username;
        let pwd :string = user_info.pwd;
        let storname = user_info.storname;
        
        //let user :any = await userInfoModel.find({where:{loginName:loginName,pwd:pwd}});
        
        let roles:any = await rolesModel.findAll({where:{loginName:loginName,pwd:pwd}});
        
        let result:any[] = [];
        
        roles.map((item,i) => {
            result[i] = item.roleCode;
        });
        
        return {result:result,storname:storname};
    }



}


let motorService = new MotorService();
export default motorService;